$(function(){
	
	asyncData.intialList();/*获取联系人*/
	button.mail_list();
	add.addteam();/*添加组*/
    add.newteam();
    bindEvent.newpage();
    bindEvent.checkall();
    bindEvent.saveteam();
    bindEvent.delet_people();
    bindEvent.export_list();
    bindEvent.import_list();
    bindEvent.find_people();

    
});
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
};/*获取地址栏URL*/
var asyncData={

	intialList:function(){
       $.getJSON("http://localhost:8080/contacts/contact/queryAll",function(contacts){
       	render.initList(contacts);
         // 	var i=0;
       		// for(i=0;i<contacts.length;i++){
         //    $("table").append('<tr><td><input type="checkbox"></td>'
         //  	+'<td>'+contacts.contacts[i].name+'</td>'
         //  	+'<td>'+contacts.contacts[i].email+'</td>'
         //  	+'<td>'+contacts.contacts[i].tel+'</td>'
         //  	+'<td>'+contacts.contacts[i].group+'</td></tr>'
         //  	)};
       })
	}
};
var render={
	initList:function(contacts){     
			
     $.each(contacts.contacts,function(idx,item){
     	
     	$("table").append('<tr id="'+item.cid+'"><td><input type="checkbox"></td><td><a href="people_message.html?id='+item.cid+'">'+item.name+'</a></td><td><a href="people_message.html?id='+item.cid+'">'+item.email+'</td><td>'
     		+item.tel+'</a></td><td><a href="people_message.html?id='+item.cid+'">'+item.group+'</a></td></tr>');       	         
       })
	}
};
function myrefresh()
{
   window.location.reload();
}
var bindEvent={
	newpage:function(){
		$("#new_people").on("click",function(){
			    window.location.href="new_people.html";
		})
	},
	checkall:function(){
		// $("#checkall").on("click",function(){
		// 	if($("#checkall").prop("checked")==false){
		// 	$("table input[type=checkbox]").attr("checked","checked");
		// }
		// else{
		// 	$("table input[type=checkbox]").removeAttr("checked");
		// }
		// })
	},
	saveteam:function(){
		$("#save").on("click",function(){
			var team_name=$("#new_team").val();			
			$.post("http://localhost:8080/contacts/group/add",{
				data:team_name
			})
		})
	},
	delet_people:function(){
		$("#delete").on("click",function(){
			layer.open({
				style: 'width:60%;font-size:14px;',
				content: '确定删除联系人？',
				btn: ['确认', '取消'],
				shadeClose: false,
				btn1:function(){
					
			var cid={};
			var entriesArr=[];
			 var tr=$(".table tr input:checkbox:checked").parent().parent();
			   for(var i = 0; i < tr.length; i++){
        var entryData = {
            cid:$(tr[i]).attr("id"),
        };
        entriesArr.push(entryData);
        }
        gid.manPlus=entriesArr;
			$.ajax({
        url: "http://localhost:8080/contacts/contact/delete",
        type: 'post',
        dataType: 'JSON',
        data:{"data": JSON.stringify(cid)},
        
        success:function(data){
                   var text = data.result;
                  if(text==0){
                    alert("success");
                  }else{
                   alert("error");
                  }
                }
       

    });
           myrefresh();
				}
				
			})

		})		
	},
import_list:function(){
        $("#import").on("click",function(){
        	$("#import_people").click();

        })
},
export_list:function(){
	$("#export").on("click",function(){
		$("#export_people").modal("toggle");
		$("#check_import").on("click",function(){
			var codeFormat=$(".modal-body .code input:checked").val();
			var fileFormat=$(".modal-body .file input:checked").val();
			var data={};
			data.codeFormat=codeFormat;
			data.fileFormat=fileFormat;
		$.ajax({
		url: "http://localhost:8080/contacts/export",
		type: 'post',
		// dataType: 'JSON',
		data:{"codeFormat":codeFormat,"fileFormat":fileFormat},
	    success:function(data){
		           var text = data.result;
		          if(text==0){
		          	 alert("导出成功");
		          }else{
		          	alert("导出失败");
		          }
		       	}
		// error:function(error){
		//        M_ContactsByCurriculumDesign.showConfirmModal("error","连接不上服务器！");
		//       }

	});
	window.location="http://localhost:8080/contacts/export?codeFormat="+codeFormat+"&fileFormat="+fileFormat ;
	


		})
	})
},
find_people:function(){
	$("#find_people").on("click",function(){
		var find_message=$("#find_message").val();
		$.ajax({
		url: "http://localhost:8080/contacts/contact/likeQuery",
		type: 'post',
		// dataType: 'JSON',
		data:{"data":find_message},
	    success:function(data){
		           var text = data.result;
		          if(text==0){
		          	 alert("导出成功");
		          }else{
		          	alert("导出失败");
		          }
		       	}
		// error:function(error){
		//        M_ContactsByCurriculumDesign.showConfirmModal("error","连接不上服务器！");
		//       }

	});
	})
}


};
var button={
	mail_list:function(){
		$("#mail_list").on("click",function(){
			window.location.href="index.html"
		})
	}
};
var add={
    addteam:function(){
        $("#add").on("click",function(){
            $("#mymodal").modal("toggle");
        })
    },
    newteam:function(){
    	     $("#team_list").append('<li><a href="group_list.html">'+"所有分组"+'</li>');
    	$.getJSON("http://localhost:8080/contacts/group/queryAll",function(groups){
    		        $.each(groups.groups,function(idx,item){
    		        	$("#team_list").append('<li><a href="group_people.html?id='+item.gid+'">'+item.groupName+'</li>');
    		        })
                   
    	})
         
        $("#save").on("click",function(){
            $("#team_list").append('<li>'+$("#new_team").val()+'</li>');
             $(".choose_select").append('<label>'+"<input type='checkbox'>"+$("#new_team").val()+'</label>');
        })
    }
};
	